package arithmetic.socket.nio;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.FileLock;

/**
 * nio的
 * 1.MappedByteBuffer mmap的方式直接写文件
 * 2.  channel.lock()
 * @author jiangfeng
 * Created on 2020-03-26
 */
public class NioTest3 {
    public static void main(String[] args) throws Exception {

        RandomAccessFile randomAccessFile = new RandomAccessFile("src/main/java/arithmetic/socket/nio/nio3.txt", "rw");

        FileChannel fileChannel = randomAccessFile.getChannel();
        // fileChannel 可以获取锁,来控制多线程读写
        //FileLock lock = fileChannel.lock();
        //lock.release();

        MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE,0,10);

        mappedByteBuffer.put(0,(byte)'a');
        mappedByteBuffer.put(2,(byte)'a');
        mappedByteBuffer.put(4,(byte)'a');

        fileChannel.close();

    }
}
